package edu.beyond.employment.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.beyond.employment.commom.util.AppUtil;
import edu.beyond.employment.company.dao.ApplicationDao;
import edu.beyond.employment.company.dao.PositionDao;
import edu.beyond.employment.company.entity.ApplicationEntity;
import edu.beyond.employment.company.entity.PositionEntity;
import edu.beyond.employment.talent.dao.ResumeDao;
import edu.beyond.employment.talent.entity.ResumeEntity;

/**
 * Position Servlet控制层
 */
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionDao positionDao = new PositionDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取页面传来的参数
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preupdate".equals(command)) {
			preupdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 列举所有的职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取页面传来的参数

		// 调用业务逻辑
		List<PositionEntity> positionList = new PositionDao().list();
		// 转向页面
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/position-list.jsp").forward(request, response);
	}

	/**
	 * 根据职位id查询所有职位信息详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (!AppUtil.isLoggin(request)) {
			request.setAttribute("sessionClosed", "请先登录，再执行简历投递！");
			request.getRequestDispatcher("view/talent/talent-login.jsp").forward(request, response);
		} else {
			// 获取页面传来的参数
			int id = Integer.parseInt(request.getParameter("id"));
			// 调用业务逻辑
			PositionEntity position = new PositionDao().detail(id);
			int talentId = AppUtil.getTalentId(request, response);
			ApplicationEntity application = applicationDao.isApplied(talentId, id);
			List<ResumeEntity> resumeList = resumeDao.list(talentId);
			// 转向页面

			request.setAttribute("position", position);
			request.setAttribute("application", application);
			request.setAttribute("resumeList", resumeList);
			request.getRequestDispatcher("view/company/position-detail.jsp").forward(request, response);
		}
	}

	/**
	 * 发布职位
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取页面传来的参数
		int companyId = AppUtil.getCompanyId(request, response);
		String name = request.getParameter("name");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		// 调用业务逻辑
		PositionEntity position = new PositionEntity();
		position.setCompany$id(companyId);
		position.setName(name);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		position.setDuty(duty);
		position.setAbility(ability);
		int row = positionDao.add(position);
		// 转向页面
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 职位删除
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		int row = positionDao.delete(id);
		// 3、转向
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 职位修改前的查询
	 */
	private void preupdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		PositionEntity position = positionDao.get(id);
		// 3、转向
		if (position == null) {
			request.setAttribute("error", "该职位不存在~");
			int companyId = AppUtil.getCompanyId(request, response);
			List<PositionEntity> positionList = positionDao.listById(companyId);
			request.setAttribute("positionList", positionList);
			request.getRequestDispatcher("view/company/main.jsp").forward(request, response);
		} else {
			request.setAttribute("position", position);
			request.getRequestDispatcher("view/company/position-update.jsp").forward(request, response);
		}
	}

	/**
	 * 进行职位真正的修改
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");

		// 2、调用业务逻辑
		PositionEntity position = new PositionEntity();
		position.setId(id);
		position.setName(name);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		position.setDuty(duty);
		position.setAbility(ability);
		int row = positionDao.update(position);

		// 3、转向
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int row = Integer.parseInt(request.getParameter("row"));
		int companyId = AppUtil.getCompanyId(request, response);
		// 2、调用业务逻辑

		// 3、转向
		if (row == 1) {
			request.setAttribute("success", "职位发布成功！");

		} else {
			request.setAttribute("fail", "职位发布失败！");
		}
		// 回到首页是显示简历信息
		List<PositionEntity> positionList = new PositionDao().listById(companyId);
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/main.jsp").forward(request, response);
	}
}